/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pet;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;



/**
 *
 * @author Administrator
 */
public class LookPetinfoServlet extends HttpServlet {
     public void wrong1(){    //对话框提示信息
        String msg="不能为空，请输入宠物的名字！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     public void wrong2(){    //对话框提示信息
        String msg="此姓名不存在，无法修改！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="查询成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     protected void doGet (HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
         String petname=new String(request.getParameter("petname").getBytes("ISO-8859-1"),"UTF-8");
         if(petname.length()==0){
             wrong1();
              response.sendRedirect("http://localhost:8084/jspsx/pets/querypet.jsp");
             
         }else{
              try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql1="select * from t_pets where name='"+petname+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k=rs.getRow();
                rs.beforeFirst();
                if(k<1){
                    wrong2();
                    response.sendRedirect("http://localhost:8084/jspsx/pets/querypet.jsp");
                }else{
                    HttpSession session=request.getSession();
                    ArrayList petlist =null;
                    petlist=new ArrayList();
                    while(rs.next()){
                        PetinfoBean ff=new PetinfoBean();
                        ff.setId(rs.getString("petid"));
                        ff.setPetname(rs.getString("name"));
                        ff.setBirthdate(rs.getString("birth_date"));
                        ff.setType(rs.getString("type"));
                        ff.setOwnerid(rs.getString("owner_name"));
                        petlist.add(ff);
                        session.setAttribute("petlist",petlist);
                        right();
                        response.sendRedirect("http://localhost:8084/jspsx/pets/lookpetinfo.jsp"); 
                    } 
                }
                //实例化保存个人信息的JavaBean
                ArrayList petlist1 =null;
                    petlist1=new ArrayList();
                    HttpSession session=request.getSession();
               PetinfoBean nn=new PetinfoBean();
               nn.setPetname(petname);
               petlist1.add(nn);
               session.setAttribute("petlist1",petlist1);  
                             rs.close();
                             stmt.close();
                             con.close();
//                             right();
//                             response.sendRedirect("http://localhost:8084/jspsx/pets/lookpetinfor.jsp");   
            }catch(Exception e){
                e.printStackTrace();
            }   
         }   
     }
      protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }

    
}
